﻿using Modding;
using Modding.Blocks;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;


class JoystickBlockController : SingleInstance<JoystickBlockController>
{

    public override string Name { get; } = "Joystick Block Controller";

    private void Awake()
    {
        //添加零件初始化事件委托
        Events.OnBlockInit += AddSliders;
    }


    /// <summary>是否有进阶属性</summary>
    public static bool HasEnhancement(BlockBehaviour block)
    {
        return block.MapperTypes.Exists(match => match.Key == "Joystick");
    }

    /// <summary>所有零件添加进阶属性控件</summary>
    public void AddAllSliders()
    {
        foreach (BlockBehaviour block in Machine.Active().BuildingBlocks.FindAll(block => !HasEnhancement(block)))
        {
            AddSliders(block);
        }
    }

    /// <summary>零件添加进阶属性控件 </summary>
    private void AddSliders(Block block)
    {
        //#if DEBUG
        //            ConsoleController.ShowMessage("on block init");
        //#endif
        BlockBehaviour blockbehaviour = block.BuildingBlock.InternalObject;
        if (!HasEnhancement(blockbehaviour))
            AddSliders(blockbehaviour);
    }
    /// <summary>零件添加进阶属性控件 </summary>
    private void AddSliders(Transform block)
    {
        BlockBehaviour blockbehaviour = block.GetComponent<BlockBehaviour>();
        if (!HasEnhancement(blockbehaviour))
            AddSliders(blockbehaviour);
    }
    /// <summary>零件添加进阶属性控件 </summary>
    private void AddSliders(BlockBehaviour block)
    {
        //#if DEBUG
        //            ConsoleController.ShowMessage(string.Format("Block ID: {0}", block.BlockID.ToString()));
        //#endif

        if (dic_EnhancementBlock.ContainsKey(block.BlockID))
        {
            var EB = dic_EnhancementBlock[block.BlockID];

            if (block.GetComponent(EB) == null)
            {
                block.gameObject.AddComponent(EB);
            }
        }
    }

    /// <summary>模块扩展脚本字典   通过字典自动为模块加载扩展脚本</summary>
    public Dictionary<int, Type> dic_EnhancementBlock = new Dictionary<int, Type>
        {
            {(int)BlockType.Piston,typeof(PistonScript) },
            { (int)BlockType.CogMediumPowered,typeof(CogScript)},
            //{(int)BlockType.SpinningBlock,typeof(SpinningScript) },
            {(int)BlockType.SteeringHinge,typeof(SteeringHinge) },
            {(int)BlockType.SteeringBlock,typeof(SteeringHinge) },
            //{(int)BlockType.Suspension,typeof(SuspensionScript) },
            //{ (int)BlockType.Flamethrower,typeof(FlamethrowerScript)},
            {(int)BlockType.Wheel,typeof(/*WheelScript*/CogScript) },
            {(int)BlockType.LargeWheel,typeof(/*WheelScript*/CogScript) },
            //{(int)BlockType.LargeWheelUnpowered,typeof(WheelScript) },
            //{(int)BlockType.WheelUnpowered,typeof(WheelScript) },
            { (int)BlockType.WaterCannon,typeof(WaterCannonScript) },
        { (int)BlockType.BallJoint,typeof(testScript)},
        };

}


